/*
 @Date: 2022/4/29
 @Desc: 咨询
 */

import UIKit
import DZNEmptyDataSet

class IPNewsViewController: UIViewController {
    
    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
    @IBOutlet weak var collectionView: UICollectionView!
    var listAry = [Any]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureCollectionView()
        let path = Bundle.main.path(forResource: "zixuan", ofType: "json")
        let url = URL(fileURLWithPath: path!)
        do {
            let data = try Data(contentsOf: url)
            let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
            let jsonArr = jsonData as! NSArray
            print(jsonArr)
            listAry = jsonArr as! [Any]
        } catch let error as Error? {
            print("读取本地数据出现错误!",error)
        }

        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
    }
    
}
//MARK: UICollectionViewDataSource UICollectionViewDelegate
extension IPNewsViewController: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return listAry.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: IPNewsCell.reuseId, for: indexPath) as! IPNewsCell
        let dic = listAry[indexPath.item] as! NSDictionary
        
        cell.newsTitleLabel.text = dic["title"] as? String
        cell.updateLabel.text = dic["time"] as? String
        cell.viewNumLabel.text = dic["liulan"] as? String
        cell.newsCover.image = UIImage.init(named: (dic["image"] as? String)!)
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let detail = IPNewsDetailViewController()
        let dic = listAry[indexPath.item] as! NSDictionary
        detail.detailDic = dic
        self.navigationController?.pushViewController(detail, animated: true)
    }
}

//MARK: DZNEmptyDataSetSource
extension IPNewsViewController: DZNEmptyDataSetSource {
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let str = "空空如也～"
        let dict: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 16),.foregroundColor: UIColor.ip_text_color]
        return NSAttributedString.init(string: str, attributes: dict)
    }
}

private extension IPNewsViewController {
    
    func configureCollectionView(){
        
        collectionView.backgroundColor = UIColor.systemGroupedBackground
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        registerView()
        calculateCellSize()
    }
    
    func registerView(){
        let cellNib = UINib.init(nibName: "IPNewsCell", bundle: .main)
        collectionView.register(cellNib, forCellWithReuseIdentifier: IPNewsCell.reuseId)
    }
    
    func calculateCellSize(){
        let sectionEdge: CGFloat = 0
        let sp: CGFloat = 12
        /// 一排3个
        let num = 1
        let totalWidth = UIScreen.main.bounds.width
        let width = totalWidth - CGFloat(num - 1) * sp - 2 * sectionEdge
        let height: CGFloat = 115
        let size = CGSize.init(width: width, height: height)
        flowLayout.itemSize = size
        flowLayout.minimumLineSpacing = 10
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets.init(top: 10, left: 0, bottom: 10, right: 0)
    }
    
    
    
}
